package cn.com.customer.manager.ui.start


import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.view.Window
import android.view.WindowManager
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import cn.com.customer.manager.R
import cn.com.customer.manager.application.App
import cn.com.customer.manager.application.App.APP_STATUS_NORMAL
import cn.com.customer.manager.ui.login.AccountLoginActivity
import cn.com.customer.manager.ui.main.MainActivity
import com.xuexiang.xutil.tip.ToastUtils
import kotlinx.android.synthetic.main.activity_start.*



class StartActivity : Activity() {
    private val PERMISSION_REQUEST_CODE = 600
    private val permissions = arrayOf(
            Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE, Manifest.permission.CALL_PHONE, Manifest.permission.READ_CALL_LOG, Manifest.permission.READ_SMS
//            Manifest.permission.ACCESS_FINE_LOCATION,
//            Manifest.permission.ACCESS_COARSE_LOCATION
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        App.APP_STATUS = APP_STATUS_NORMAL
        beforeSetContentView()
        setContentView(R.layout.activity_start)
        initView(savedInstanceState)
    }

    fun beforeSetContentView() {
        //无title
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        //全屏
        window.setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
    }

    /**
     * 初始化控件
     */
    fun initView(savedInstanceState: Bundle?) {
        val anim = AlphaAnimation(0.3f, 1.0f)
        anim.duration = 3000
        anim.setAnimationListener(object : Animation.AnimationListener {

            override fun onAnimationRepeat(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {
                requestPermission()
            }

            override fun onAnimationStart(animation: Animation?) {
            }
        })
        start.startAnimation(anim)
    }


    /**
     * 进入主页面
     */
    private fun goToMain() {
        startActivity(Intent(App.getContext(), AccountLoginActivity::class.java))
        finish()

    }

    /**
     * 检查权限
     */
    private fun requestPermission() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            goToMain()
        } else {
            if (checkPermissionAllGranted(permissions)) {
                goToMain()
            } else {
                ActivityCompat.requestPermissions(this, permissions, PERMISSION_REQUEST_CODE)
            }
        }
    }

    /**
     * 检查是否拥有指定的所有权限
     */
    private fun checkPermissionAllGranted(permissions: Array<String>): Boolean {
        for (permission in permissions) {
            if (ContextCompat.checkSelfPermission(App.getContext(), permission) != PackageManager.PERMISSION_GRANTED) {
                // 只要有一个权限没有被授予, 则直接返回 false
                return false
            }
        }
        return true
    }

    /**
     * 第 3 步: 申请权限结果返回处理
     */
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        if (requestCode == PERMISSION_REQUEST_CODE) {
            var isAllGranted = true

            // 判断是否所有的权限都已经授予了
            for (grant in grantResults) {
                if (grant != PackageManager.PERMISSION_GRANTED) {
                    isAllGranted = false
                    break
                }
            }

            if (isAllGranted) {
                goToMain()
            } else {
                // 弹出对话框告诉用户需要权限的原因, 并引导用户去应用权限管理中手动打开权限按钮
                ToastUtils.toast( "您好，当前应用缺少必要权限，请到 “应用信息 -> 权限” 中授予所需权限。")
                finish()
            }
        }
    }

}
